﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

public partial class Controls_Comment : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
        }
    }

    private void PopulateControls()
    {
        int howManyPages = 0;
        string productID = Request.QueryString["ProductID"];
        string page = Request.QueryString["Page"] ?? "1";
        string firstPageUrl = Link.ToProduct(productID);
        string pagerFormat = Link.ToProductWithComment(productID,"{0}"); ;
        HttpContext context = HttpContext.Current;
        if (context != null)
        {
            if (context.User.Identity.IsAuthenticated)
                pnComment.Visible = true;
            else
                pnComment.Visible = false;

            string userName = context.User.Identity.Name;
            RepComment.DataSource = Comments.GetByProductID(Convert.ToInt32(productID), page, out howManyPages);
            RepComment.DataBind();

            pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Comments cm = new Comments();
        HttpContext context = HttpContext.Current;
        if (context != null && txtComment.Text!="")
        {
            cm.ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
            cm.UserName = context.User.Identity.Name;
            cm.Comment = txtComment.Text;
            cm.Date = DateTime.Now;
            if (Comments.Add(cm))
                PopulateControls();
        }
    }
}